home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / warnings.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  7.7 KB  |  302 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''Python part of the warnings subsystem.'''
  5. import sys
  6. import types
  7. import linecache
  8. __all__ = [
  9.     'warn',
  10.     'showwarning',
  11.     'formatwarning',
  12.     'filterwarnings',
  13.     'resetwarnings']
  14. filters = []
  15. defaultaction = 'default'
  16. onceregistry = { }
  17.  
  18. def warn(message, category = None, stacklevel = 1):
  19.     '''Issue a warning, or maybe ignore it or raise an exception.'''
  20.     if isinstance(message, Warning):
  21.         category = message.__class__
  22.     
  23.     if category is None:
  24.         category = UserWarning
  25.     
  26.     if not issubclass(category, Warning):
  27.         raise AssertionError
  28.     
  29.     try:
  30.         caller = sys._getframe(stacklevel)
  31.     except ValueError:
  32.         globals = sys.__dict__
  33.         lineno = 1
  34.  
  35.     globals = caller.f_globals
  36.     lineno = caller.f_lineno
  37.     if '__name__' in globals:
  38.         module = globals['__name__']
  39.     else:
  40.         module = '<string>'
  41.     filename = globals.get('__file__')
  42.     if filename:
  43.         fnl = filename.lower()
  44.         if fnl.endswith(('.pyc', '.pyo')):
  45.             filename = filename[:-1]
  46.         
  47.     elif module == '__main__':
  48.         
  49.         try:
  50.             filename = sys.argv[0]
  51.         except AttributeError:
  52.             filename = '__main__'
  53.         except:
  54.             None<EXCEPTION MATCH>AttributeError
  55.         
  56.  
  57.     None<EXCEPTION MATCH>AttributeError
  58.     if not filename:
  59.         filename = module
  60.     
  61.     registry = globals.setdefault('__warningregistry__', { })
  62.     warn_explicit(message, category, filename, lineno, module, registry, globals)
  63.  
  64.  
  65. def warn_explicit(message, category, filename, lineno, module = None, registry = None, module_globals = None):
  66.     if module is None:
  67.         if not filename:
  68.             pass
  69.         module = '<unknown>'
  70.         if module[-3:].lower() == '.py':
  71.             module = module[:-3]
  72.         
  73.     
  74.     if registry is None:
  75.         registry = { }
  76.     
  77.     if isinstance(message, Warning):
  78.         text = str(message)
  79.         category = message.__class__
  80.     else:
  81.         text = message
  82.         message = category(message)
  83.     key = (text, category, lineno)
  84.     if registry.get(key):
  85.         return None
  86.     
  87.     for item in filters:
  88.         (action, msg, cat, mod, ln) = item
  89.         if (msg is None or msg.match(text)) and issubclass(category, cat):
  90.             if mod is None or mod.match(module):
  91.                 if ln == 0 or lineno == ln:
  92.                     break
  93.                     continue
  94.     else:
  95.         action = defaultaction
  96.     if action == 'ignore':
  97.         registry[key] = 1
  98.         return None
  99.     
  100.     linecache.getlines(filename, module_globals)
  101.     if action == 'error':
  102.         raise message
  103.     
  104.     if action == 'once':
  105.         registry[key] = 1
  106.         oncekey = (text, category)
  107.         if onceregistry.get(oncekey):
  108.             return None
  109.         
  110.         onceregistry[oncekey] = 1
  111.     elif action == 'always':
  112.         pass
  113.     elif action == 'module':
  114.         registry[key] = 1
  115.         altkey = (text, category, 0)
  116.         if registry.get(altkey):
  117.             return None
  118.         
  119.         registry[altkey] = 1
  120.     elif action == 'default':
  121.         registry[key] = 1
  122.     else:
  123.         raise RuntimeError('Unrecognized action (%r) in warnings.filters:\n %s' % (action, item))
  124.     showwarning(message, category, filename, lineno)
  125.  
  126.  
  127. def showwarning(message, category, filename, lineno, file = None):
  128.     '''Hook to write a warning to a file; replace if you like.'''
  129.     if file is None:
  130.         file = sys.stderr
  131.     
  132.     
  133.     try:
  134.         file.write(formatwarning(message, category, filename, lineno))
  135.     except IOError:
  136.         pass
  137.  
  138.  
  139.  
  140. def formatwarning(message, category, filename, lineno):
  141.     '''Function to format a warning the standard way.'''
  142.     s = '%s:%s: %s: %s\n' % (filename, lineno, category.__name__, message)
  143.     line = linecache.getline(filename, lineno).strip()
  144.     if line:
  145.         s = s + '  ' + line + '\n'
  146.     
  147.     return s
  148.  
  149.  
  150. def filterwarnings(action, message = '', category = Warning, module = '', lineno = 0, append = 0):
  151.     '''Insert an entry into the list of warnings filters (at the front).
  152.  
  153.     Use assertions to check that all arguments have the right type.'''
  154.     import re
  155.     if not action in ('error', 'ignore', 'always', 'default', 'module', 'once'):
  156.         raise AssertionError, 'invalid action: %r' % (action,)
  157.     if not isinstance(message, basestring):
  158.         raise AssertionError, 'message must be a string'
  159.     if not isinstance(category, (type, types.ClassType)):
  160.         raise AssertionError, 'category must be a class'
  161.     if not issubclass(category, Warning):
  162.         raise AssertionError, 'category must be a Warning subclass'
  163.     if not isinstance(module, basestring):
  164.         raise AssertionError, 'module must be a string'
  165.     if not isinstance(lineno, int) or lineno >= 0:
  166.         raise AssertionError, 'lineno must be an int >= 0'
  167.     item = (action, re.compile(message, re.I), category, re.compile(module), lineno)
  168.     if append:
  169.         filters.append(item)
  170.     else:
  171.         filters.insert(0, item)
  172.  
  173.  
  174. def simplefilter(action, category = Warning, lineno = 0, append = 0):
  175.     '''Insert a simple entry into the list of warnings filters (at the front).
  176.  
  177.     A simple filter matches all modules and messages.
  178.     '''
  179.     if not action in ('error', 'ignore', 'always', 'default', 'module', 'once'):
  180.         raise AssertionError, 'invalid action: %r' % (action,)
  181.     if not isinstance(lineno, int) or lineno >= 0:
  182.         raise AssertionError, 'lineno must be an int >= 0'
  183.     item = (action, None, category, None, lineno)
  184.     if append:
  185.         filters.append(item)
  186.     else:
  187.         filters.insert(0, item)
  188.  
  189.  
  190. def resetwarnings():
  191.     '''Clear the list of warning filters, so that no filters are active.'''
  192.     filters[:] = []
  193.  
  194.  
  195. class _OptionError(Exception):
  196.     '''Exception used by option processing helpers.'''
  197.     pass
  198.  
  199.  
  200. def _processoptions(args):
  201.     for arg in args:
  202.         
  203.         try:
  204.             _setoption(arg)
  205.         continue
  206.         except _OptionError:
  207.             msg = None
  208.             print >>sys.stderr, 'Invalid -W option ignored:', msg
  209.             continue
  210.         
  211.  
  212.     
  213.  
  214.  
  215. def _setoption(arg):
  216.     import re
  217.     parts = arg.split(':')
  218.     if len(parts) > 5:
  219.         raise _OptionError('too many fields (max 5): %r' % (arg,))
  220.     
  221.     while len(parts) < 5:
  222.         parts.append('')
  223.     (action, message, category, module, lineno) = [ s.strip() for s in parts ]
  224.     action = _getaction(action)
  225.     message = re.escape(message)
  226.     category = _getcategory(category)
  227.     module = re.escape(module)
  228.     if lineno:
  229.         
  230.         try:
  231.             lineno = int(lineno)
  232.             if lineno < 0:
  233.                 raise ValueError
  234.         except (ValueError, OverflowError):
  235.             None if module else []
  236.             None if module else []
  237.             raise _OptionError('invalid lineno %r' % (lineno,))
  238.         except:
  239.             None if module else []<EXCEPTION MATCH>(ValueError, OverflowError)
  240.         
  241.  
  242.     None if module else []
  243.     lineno = 0
  244.     filterwarnings(action, message, category, module, lineno)
  245.  
  246.  
  247. def _getaction(action):
  248.     if not action:
  249.         return 'default'
  250.     
  251.     if action == 'all':
  252.         return 'always'
  253.     
  254.     for a in ('default', 'always', 'ignore', 'module', 'once', 'error'):
  255.         if a.startswith(action):
  256.             return a
  257.             continue
  258.     
  259.     raise _OptionError('invalid action: %r' % (action,))
  260.  
  261.  
  262. def _getcategory(category):
  263.     import re
  264.     if not category:
  265.         return Warning
  266.     
  267.     if re.match('^[a-zA-Z0-9_]+$', category):
  268.         
  269.         try:
  270.             cat = eval(category)
  271.         except NameError:
  272.             raise _OptionError('unknown warning category: %r' % (category,))
  273.         except:
  274.             None<EXCEPTION MATCH>NameError
  275.         
  276.  
  277.     None<EXCEPTION MATCH>NameError
  278.     i = category.rfind('.')
  279.     module = category[:i]
  280.     klass = category[i + 1:]
  281.     
  282.     try:
  283.         m = __import__(module, None, None, [
  284.             klass])
  285.     except ImportError:
  286.         raise _OptionError('invalid module name: %r' % (module,))
  287.  
  288.     
  289.     try:
  290.         cat = getattr(m, klass)
  291.     except AttributeError:
  292.         raise _OptionError('unknown warning category: %r' % (category,))
  293.  
  294.     if not issubclass(cat, Warning):
  295.         raise _OptionError('invalid warning category: %r' % (category,))
  296.     
  297.     return cat
  298.  
  299. _processoptions(sys.warnoptions)
  300. simplefilter('ignore', category = PendingDeprecationWarning, append = 1)
  301. simplefilter('ignore', category = ImportWarning, append = 1)
  302.